<?php
class Pager implements Countable, IteratorAggregate{
    
    // 参数配置
    protected $config = array(
    	'page_url' => '',		// 当前页数地址
    	'page_var' => 'p',		// 页数变量
    	'page_size' => 20, 		// 每页显示记录数
    	'page_size_var' => 'pagesize', 		// 每页显示记录数变量
    	'page_size_sel' => array(10,20,30,50,100), // 每页显示个数数组
    	'params' => '',			// 页数跳转时要带的参数
    	'view' => 'pagination/default', // 使用的视图
    	'total_pages' => 0, 	// 分页总页面数
    	'total_rows' => 0,		// 总行数
    	'cur_page' => 1,  		// 当前页数
    	'str_pre_page' => '上一页',
    	'str_next_page' => '下一页'
    );
    
    // 执行的SQL
    protected $sql = '';
    
    // 显示数据集
    protected $items = array();
    
   	// 页面Controller
   	protected $ctl = NULL;
        
    function __construct($config = array()) 
    {
    	foreach ($config as $key => $val)
		{
			if (isset($this->$key))
			{
				$this->$key = $val;
			}
		}
    	
    	$this->ctl = CI_Controller::get_instance();
    	$this->config['page_url'] = '/'.$this->ctl->uri->uri_string();
    	
    	$this->config['params'] = $this->ctl->input->server('QUERY_STRING');
    	$reg = '/'.$this->config['page_var'].'=\w*&?/';
    	$this->config['params'] = trim(preg_replace($reg, '', $this->config['params']));
    	$sep = empty($this->config['params']) ? '' : '&';
    	$this->config['params'] .= $sep;    
    	$this->config['cur_page'] = (int)$this->ctl->input->get($this->config['page_var'], TRUE);
    	empty($this->config['cur_page']) AND $this->config['cur_page'] = 1;
    	$page_size = (int)$this->ctl->input->get($this->config['page_size_var'], TRUE);
    	$page_size > 0 AND $this->config['page_size'] = $page_size;
    	
    	$this->config['total_rows'] = $this->ctl->db->query($this->sql)->num_rows();
    	$this->config['total_pages'] = ceil($this->config['total_rows'] / $this->config['page_size']);
    	
    	$this->config['cur_page'] <= 0 AND $this->config['cur_page'] = 1;
    	$this->config['cur_page'] > $this->config['total_pages'] AND $this->config['cur_page'] = $this->config['total_pages'];
    	$start_row = ($this->config['cur_page'] - 1) * $this->config['page_size'];
    	$start_row < 0 AND $start_row = 1;
    	$this->sql = $this->sql . ' LIMIT ' . $start_row . ',' . $this->config['page_size'];
    	
    	$this->items = $this->ctl->db->query($this->sql)->result_array();
    }
    
    /*
     * 前台输出分页HTML
     */
    function __toString()
    {
    	// 构造数据
    	$data = array('pager' => &$this);
    	
    	// 显示视图
    	$this->ctl->load->view($this->config['view'], $data);
    	
    	return '';
    }

    function __get($k)
    {
    	return isset($this->config[$k]) ? $this->config[$k] : '';
    }
    /**
     * 返回给 foreach 调用的接口
     *
     * @return ArrayObject
     */
    public function getIterator()
    {
        return $this->items;
    }

    /**
     * 返回 count 函数获得的值
     *
     * @return integer
     */
    public function count()
    {
        return count($this->items);
    }
    
    /*
     * 构造URL地址
     * @param int $i 构造的页码
     */
    public function url($i)
    {
    	return $this->config['page_url'].'?'.$this->config['params'].$this->config['page_var'].'='.$i;
    }
}
?>
